package com.gxwzxy.medic.dao.Impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.gxwzxy.medic.dao.MedicalDao;
import com.gxwzxy.medic.entity.Institution;
import com.gxwzxy.medic.entity.Medical;
import com.gxwzxy.medic.entity.otherentity.S201;
import com.gxwzxy.medic.until.Connect;

public class MedicalDaoImpl implements MedicalDao {
	
	//声明jdbc连接变量
	ResultSet rs = null;
	Connection conn = null;
	PreparedStatement ps = null;
	
	/**
	 * 查询医疗机构个数
	 */
	@Override
	public int findMedicalSize() {
		String sql = "select COUNT(*) from medical";
		int i = 0;
		try {
			//调用静态方法
			rs = Connect.executeF(sql);
			if(rs.next()) {
				i = rs.getInt(1);
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			Connect.close(conn, ps, rs);
		}
		return i;
	}

	/**
	 * 分页查询，start是开始的页数，size是一页行有多少数据
	 */
	@Override
	public List<Medical> findMedicalIndex(int start, int size) {
		// 声明变量
		ArrayList<Medical> am = new ArrayList<>();
		String sql = null;
		try {
			// 创建Sql语句
			sql = "select * from medical limit ?,?";
			// 执行sql语句
			rs =Connect.executeF(sql, start,size);
			// 获取结果
			while (rs.next()) {
				Medical m = new Medical();
				m.setMedicalcode(rs.getString("medicalcode"));
				m.setOrganizationcode(rs.getString("organizationcode"));
				m.setMedicalname(rs.getString("medicalname"));
				m.setRegioncode(rs.getString("regioncode"));
				m.setAreacode(rs.getString("areacode"));
				m.setAffiliation(rs.getString("affiliation"));
				m.setGrade(rs.getString("grade"));
				m.setDeclaretype(rs.getString("declaretype"));
				m.setApprovetype(rs.getString("approvetype"));
				m.setEconomytype(rs.getString("economytype"));
				m.setHealthorganization(rs.getString("healthorganization"));
				m.setSponsorunit(rs.getString("sponsorunit"));
				m.setEstablishtime(rs.getString("establishtime"));
				m.setCharge(rs.getString("charge"));
				m.setRegistermoney(rs.getString("registermoney"));
				am.add(m);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			Connect.close(conn, ps, rs);
		}
		// 关闭连接
		return am;
	}

	/**
	 * 查询S201关系表
	 */
	@Override
	public List<S201> findS201() {
		//声明变量
		String sql ="select * from s201";
		ArrayList<S201> as = new ArrayList<S201>();
		try {
			// 执行sql语句
			rs =Connect.executeF(sql);
			// 获取结果
			while (rs.next()) {
				S201 s = new S201(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4));
				as.add(s);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			Connect.close(conn, ps, rs);
		}
		return as;
	}

	/**
	 * 保存医疗机构
	 */
	@Override
	public boolean saveMedical(Medical m) {
		//创建SQL语句
		String sql = "insert into medical(medicalcode,organizationcode,medicalname,regioncode,"+
		             "areacode,affiliation,grade,declaretype,approvetype,economytype,healthorganization,"+
				     "sponsorunit,establishtime,charge,registermoney) value(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		int i = Connect.executeDML(sql,m.getMedicalcode(),m.getOrganizationcode(),m.getMedicalname(),m.getRegioncode()
				,m.getAreacode(),m.getAffiliation(),m.getGrade(),m.getDeclaretype(),m.getApprovetype(),m.getEconomytype()
				,m.getHealthorganization(),m.getSponsorunit(),m.getEstablishtime(),m.getCharge(),m.getRegistermoney());
		if(i!=-1) {
			return true;
		}else {
			return false;
		}
	}

	/**
	 * 根据meidcalcode查询医疗机构
	 */
	@Override
	public List<Medical> findMedicalByMcode(String medicalcode) {
		//声明变量
		ArrayList<Medical> am = new ArrayList<Medical>();
		String sql = null;
		try {
			//创建sql语句
			sql = "select * from medical where medicalcode = ?";
			rs = Connect.executeF(sql, medicalcode);
			while(rs.next()) {
				Medical m = new Medical();
				m.setMedicalcode(rs.getString("medicalcode"));
				m.setOrganizationcode(rs.getString("organizationcode"));
				m.setMedicalname(rs.getString("medicalname"));
				m.setRegioncode(rs.getString("regioncode"));
				m.setAreacode(rs.getString("areacode"));
				m.setAffiliation(rs.getString("affiliation"));
				m.setGrade(rs.getString("grade"));
				m.setDeclaretype(rs.getString("declaretype"));
				m.setApprovetype(rs.getString("approvetype"));
				m.setEconomytype(rs.getString("economytype"));
				m.setHealthorganization(rs.getString("healthorganization"));
				m.setSponsorunit(rs.getString("sponsorunit"));
				m.setEstablishtime(rs.getString("establishtime"));
				m.setCharge(rs.getString("charge"));
				m.setRegistermoney(rs.getString("registermoney"));
				am.add(m);
			}
		}catch(Exception e){
			e.printStackTrace();
		}//关闭连接
		finally {
			Connect.close(conn, ps, rs);
		}
		return am;
	}

	/**
	 * 根据meidcalcode修改医疗机构
	 */
	@Override
	public boolean updateMedical(Medical m) {
		//创建sql语句
		String sql = "update medical set organizationcode = ? ,medicalname = ? ,"
				+ "regioncode = ? ,areacode = ? ,affiliation = ? ,grade = ? ,declaretype = ? ,"
				+ "approvetype = ? ,economytype = ? ,healthorganization = ? ,sponsorunit = ? ,"
				+ "establishtime = ? ,charge = ? ,registermoney = ? where medicalcode = ?";
		int i = Connect.executeDML(sql, m.getOrganizationcode() ,m.getMedicalname() ,m.getRegioncode()
				,m.getAreacode() ,m.getAffiliation() ,m.getGrade() ,m.getDeclaretype() ,m.getApprovetype()
				,m.getEconomytype() ,m.getHealthorganization() ,m.getSponsorunit() ,m.getEstablishtime()
				,m.getCharge() ,m.getRegistermoney() ,m.getMedicalcode());
		if(i!=-1) {
			return true;
		}else {
			return false;
		}
	}

	@Override
	public boolean deleteMedical(String medicalcode) {
		//创建sql语句
		String sql = "delete from medical where medicalcode = ?";
		int  i =Connect.executeDML(sql, medicalcode);
		if(i!=-1) {
			return true;
		}else {
			return false;
		}
	}
	
}
